package zk.weart.core.aliyun.oss;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zk.weart.core.aliyun.AliyunConfig;

import java.io.InputStream;


/**
 * @Author zjk
 * @Description:
 * @Date 2022/5/12 14:38
 **/
@Component
@Slf4j
public class AliOssUtil {

    @Autowired
    AliyunConfig aliyunConfig;

    /**
     * 功能描述:
     * @author zkj
     * @date 2022/5/12
     * @param folder  bucket上的目录
     * @param fileName bucket 最终文件名
     * @param inputStream
     * @param isPublic 是否公用读写
     * @param expires 过期时间
     * @return java.lang.String
     */
    public String putOss(String folder,
                         String fileName,
                         InputStream inputStream, Boolean isPublic, Integer expires){

        String endpoint = aliyunConfig.getOssConfig().getEndpoint();
        String bucket = aliyunConfig.getOssConfig().getBucket();
        String accessKeyId =  aliyunConfig.getAccessKeyId();
        String accessKeySecret = aliyunConfig.getAccessKeySecret();

        OSSTools ossTools =  new OSSTools();

        String fileKey = folder+"/"+fileName;

        ossTools.putOSS(
                endpoint,
                accessKeyId,
                accessKeySecret,
                bucket,
                fileKey,
                inputStream,
                isPublic);

        String fileUrl = ossTools.getFileUrl(
                endpoint,
                accessKeyId,
                accessKeySecret,
                bucket,
                fileName,
                expires
                );

        return  fileUrl;
    }

}
